package hoowin.app.wsnd.service;

import java.util.Iterator;
import java.util.List;

import hoowin.app.wsnd.dao.IStatusDao;
import hoowin.app.wsnd.model.Status;
import hoowin.basic.service.UserPool;

public class StatusService {
	private IStatusDao statusDao;
	private UserPool up;
	private TranslationService translationService;
	private DiscussionService discussionService;
	public boolean saveStatus(String content, String language, int publisherId){
		if(publisherId==0)return false;
		Status status = new Status();
		status.setContent(content);
		status.setLanguage(language);
		status.setPublisherId(publisherId);
		return statusDao.addStatus(status)>0;
	}
	private void detailizeStatusWithTranslations(Status status){
		status.setPublisher(up.getUserById(status.getPublisherId()));
		status.setTranslations(translationService.getTranslationListByStatusId(status.getId()));
	}
	private void detailizeStatusWithDiscussions(Status status){
		status.setPublisher(up.getUserById(status.getPublisherId()));
		status.setDiscussions(discussionService.getDiscussionListByStatusId(status.getId()));
	}
	public Status getStatusWithDiscussions(int statusId){
		Status status = statusDao.getStatusById(statusId);
		if(status==null)return null;
		detailizeStatusWithDiscussions(status);
		return status;
	}
	public List<Status> getMyStatusList(int userId){
		if(userId==0)return null;
		List<Status> myStatusList =  statusDao.getStatusListByPublisherId(userId);
		Iterator<Status> it = myStatusList.iterator();
		while(it.hasNext()){
			detailizeStatusWithTranslations(it.next());
		}
		return myStatusList;
	}
	public List<Status> getStatusOfFollowees(int userId){
		if(userId==0)return null;
		List<Status> followeeStatusList = statusDao.getStatusOfFollowees(userId);
		Iterator<Status> it = followeeStatusList.iterator();
		while(it.hasNext()){
			detailizeStatusWithTranslations(it.next());
		}
		return followeeStatusList;
	}
	public void setStatusDao(IStatusDao statusDao) {
		this.statusDao = statusDao;
	}
	public IStatusDao getStatusDao() {
		return statusDao;
	}
	public void setUp(UserPool up) {
		this.up = up;
	}
	public UserPool getUp() {
		return up;
	}
	public void setTranslationService(TranslationService translationService) {
		this.translationService = translationService;
	}
	public TranslationService getTranslationService() {
		return translationService;
	}
	public void setDiscussionService(DiscussionService discussionService) {
		this.discussionService = discussionService;
	}
	public DiscussionService getDiscussionService() {
		return discussionService;
	}
}
